<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader @addList="addList" :arr="list"></TodoHeader>
    <TodoMain :list="showIsSel" @delList="delList"></TodoMain>
    <TodoFooter :list="showIsSel" @getIsSel="getIsSel" @clear="clearFn"></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import "./styles/base.css"
import "./styles/index.css"

import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";


export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  methods: {
    addList(value) {
      this.list.push({
        id: this.list.length ? this.list[this.list.length - 1].id + 1 : 100,
        name: value,
        isDone: false
      })
    },
    delList(index){
      this.list.splice(index, 1)
    },
    getIsSel(isSel){
      this.isSel = isSel
    },
    clearFn(){
      this.list = this.list.filter(obj => obj.isDone === false)
      console.log(this.list);
    },

  },
  data() {
    return {
      // list: [
      //   { id: 100, name: "吃饭", isDone: true },
      //   { id: 201, name: "睡觉", isDone: false },
      //   { id: 103, name: "打豆豆", isDone: true },
      // ],
      list: JSON.parse(localStorage.getItem('list')) || [],
      isSel:'all'
    };
  },
  computed: {
    showIsSel(){
      if(this.isSel == 'no'){
        return this.list.filter(obj => obj.isDone === false) 
      }else if(this.isSel == 'yes'){
        return this.list.filter(obj => obj.isDone === true) 
      }else{
        return this.list
      }
    }
  },
  watch:{
        list:{
            handler(){
                localStorage.setItem('list',JSON.stringify(this.list))
            }
        }
    }
};
</script>